package com.yangaobo.mapper;

import com.yangaobo.dto.CoursePageDTO;
import com.yangaobo.entity.Course;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author 杨奥博
 */
@Repository
public interface CourseMapper {
    @Insert("""
            insert into cms_course(`title`, `category`, `idx`, `hours`, `info`, `deleted`, `version`, `created`, `updated`)
            values (#{title}, #{category}, #{idx}, #{hours}, #{info}, #{deleted}, #{version}, #{created}, #{updated})
            """)
    int insert(Course course);

    @Select("""
            select * from cms_course t where t.id = #{param1} and t.deleted = 0
            """)
    Course select(Long id);

    @Select("""
            <script>
            select * from cms_course t
           <where>
                <if test='title != null'> title like concat('%', #{title}, '%') and </if>
                <if test="category != null"> category like concat('%', #{category}, '%') and </if>
                t.deleted = 0
            </where>
            order by t.category, t.idx
            </script>
            """)
    List<Course> list(CoursePageDTO dto);

    @Update("""
            <script>
            update cms_course
            <set>
                <if test="title != null">title = #{title},</if>
                <if test="category != null">category = #{category},</if>
                <if test="idx != null">idx = #{idx},</if>
                <if test="hours != null">hours = #{hours},</if>
                <if test="info != null">info = #{info},</if>
                <if test="deleted != null">deleted = #{deleted},</if>
                <if test="created != null">created = #{created},</if>
                <if test="updated != null">updated = #{updated},</if>
                version=version+1
            </set>
            where id = #{id} and deleted = 0 and version = #{version}
            </script>
            """
            )
    int update(Course course);

    @Update("""
            update cms_course set deleted = 1, updated = current_timestamp where id = #{id}
            """)
    int delete(Long id);

    @Update("""
            <script>
            update cms_course set deleted = 1, updated = current_timestamp where
            <foreach collection='list' item='e' open='id in (' close= ')' separator=','>
                #{e}
            </foreach>
            </script>
            """)
    int deleteBatch(List<Long> ids);

}
